package tgc.edu.examxy.web.controller;

import java.io.File;
import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.examxy.custom.AjaxResult;
import tgc.edu.examxy.custom.DataGridParam;
import tgc.edu.examxy.custom.DataGridUtils;
import tgc.edu.examxy.entity.CurriculumPlan;
import tgc.edu.examxy.entity.EvaluationRule;
import tgc.edu.examxy.entity.Student;
import tgc.edu.examxy.entity.StudentInformation;
import tgc.edu.examxy.security.User2;
import tgc.edu.examxy.service.StudentAssessService;
import tgc.edu.examxy.service.StudentInformationService;
import tgc.edu.examxy.service.StudentService;
import tgc.edu.examxy.service.SysFileService;
import tgc.edu.examxy.web.form.EvaluationRuleForm;
import tgc.edu.examxy.web.form.StudentInformationForm;

/****
 * 我的成绩
 * @author 936642284
 *
 */
@Controller
@RequestMapping(value = "/performance")
public class PerformanceController{
	@Autowired
	private StudentAssessService assessservice;
	@Autowired
	private StudentInformationService inService;
	@Autowired
	private StudentService studentservice;
	@RequestMapping(value = "/manage")
	private String manage() {
		return "performance/manage";
	}
	
	
	@RequestMapping(value = "/page")
	@ResponseBody
	private Object page(DataGridParam param, StudentInformationForm form) {
		Sort sort = Sort.by("createDate");
		Pageable pageable = param.getPageable(sort);
		Specification<StudentInformation> spec = buildSpec(form);
		Page<StudentInformation> page = inService.findAll(spec, pageable);
		return DataGridUtils.buildResult(page);
	}


	private Specification<StudentInformation> buildSpec(StudentInformationForm form) {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    Student usernames = studentservice.findByUsername(user.getUsername());
	    
		Specification<StudentInformation> specification = new Specification<StudentInformation>() {

			private static final long serialVersionUID = 1L;
			
			@Override
			public Predicate toPredicate(Root<StudentInformation> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(usernames!=null) {
					Predicate name = cb.equal(root.get("assess").get("student").get("id"), usernames.getId());
					rules.add(name);
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		if(StringUtils.hasText(form.getSearch())) {
			Specification<StudentInformation> specsearch = new Specification<StudentInformation>() {

				private static final long serialVersionUID = 1L;

				@Override
				public Predicate toPredicate(Root<StudentInformation> root, CriteriaQuery<?> query,
						CriteriaBuilder cb) {
					HashSet<Predicate> rules = new HashSet<>();
					String search = form.getSearch();
					if(StringUtils.hasText(search)) {
						rules.add(cb.like(root.get("information").get("examination"), "%"+search+"%"));
					}
					if(StringUtils.hasText(search)) {
						rules.add(cb.like(root.get("information").get("scoringmethod"), "%"+search+"%"));
					}
					if(StringUtils.hasText(search)) {
						rules.add(cb.like(root.get("information").get("explain"), "%"+search+"%"));
					}
					try {
						double num1 = Double.valueOf(search.toString());
						if(StringUtils.hasText(search)) {
							rules.add(cb.equal(root.get("information").get("score"), num1));
						}
						if(StringUtils.hasText(search)) {
							rules.add(cb.equal(root.get("grade"), num1));
						}
						return cb.or(rules.toArray(new Predicate[rules.size()]));
					}catch (Exception e) {
						return cb.or(rules.toArray(new Predicate[rules.size()]));
					}
					
					
					
				}
			};
			return specification.and(specsearch);
		}else {
			
			return specification;
		}
		
	}
	@RequestMapping(value = "/grouppage")
	@ResponseBody
	private Object grouppage(DataGridParam param, StudentInformationForm form) {
		Sort sort = Sort.by("createDate");
		Pageable pageable = param.getPageable(sort);
		Specification<StudentInformation> spec = buildSpec(form);
		Page<StudentInformation> page = inService.findAll(spec, pageable);
		return DataGridUtils.buildResult(page);
	}
	/****
	 *  返回是否有证书
	 * @return
	 */
	@Autowired
	private SysFileService sysfileservice;
	
	/***
	 * 查找图片
	 * @return
	 */
	@RequestMapping(value= "/certificate")
	@ResponseBody
	private Object certificate() {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    Student usernames = studentservice.findByUsername(user.getUsername());
	    if(usernames.getCertificateuuid()==null) {
	    	return new AjaxResult(false, "还没有证书！");
	    }else {
	    	Boolean whether = sysfileservice.whether(usernames.getCertificateuuid());
	    	if(whether) {
	    		return new AjaxResult(usernames.getCertificateuuid());
	    	}else {
	    		return new AjaxResult(false, "证书已不存在");
	    	}
	    }
	}
}
